Upgrading Mespelare firmware to XC8
As the C18 compiler is no longer supported by Microchip and the rest of the VSCP group has migrated their code to XC8, I needed to migrate my Mespelare firmware as well. This article describes the process.
This article is a draft. Proceed at your own peril.
Migration guide
Microchip has described the process in their MPLAB C18 to XC8 C Compiler Migration Guide. It is suggested reading.
I’ll be following the Migration Guide throughout this article to migrate my code, so if you want you can follow along.
We’ll skip C18 compatibility mode, and do a proper code upgrade to the XC8 compiler.
Integer promotion
I had an issue with integer promotion behaviour in C18 before, and had to set the option to automatically promote operations to integer. In XC8 this option is no longer available, and integer promotion has become standard compiler behaviour.
Section 3.4 Integer Promotions has this to say: The MPLAB XC8 compiler cannot mimic this non-standard behavior. The results obtained from this compiler will always be consistent with the smaller data types (i.e. short, char and structure bit-fields) being promoted to either signed int or unsigned int. This means that the result of expressions using the smaller types can be different to those obtained from the same code compiled with MPLAB C18.
Yay, let’s hope so!
3.5.1 Header Files
MPLAB XC8 uses the header file _<xc.h> for all device specific information and it is likely you will need to include this header in all your C source modules. Separate assembly modules should include the header <xc.inc> to access SFRs by name._
This means in main.c and in can18F66K80.c we’ll remove the line #include <p18cxxx.h> and add #include <xc.h> instead. XC8 will now automagically include the correct header for our target device based on the device we set in the project option.
3.6 DATA TYPES AND LIMITS
The type of a plain char is _signed char on MPLAB C18 (unless the -k option is used), but unsigned char on MPLAB XC8. It is always good practice to explicitly state the signedness of char types when you define them._
Does this mean we need to change all our char variable initiations to signed char? We’ll skip that for now and keep it in mind for when our code behaves erratical.
3.13 INTERRUPTS
The definitions of interrupt vectors and service routines when using MPLAB XC8 is dif- ferent to those applicable for MPLAB C18. Encoding of the service routines also differs between the two compilers.
This means we’ll need to change our high and low interrupt code in main.c.
The old code:
#ifdef RELOCATE
#pragma code low_vector = 0x818
#else
#pragma code low_vector = 0x18
#endif
void interrupt_at_low_vector( void ) {
_asm GOTO isr_low _endasm
}
#pragma code
#pragma interruptlow isr_low
void isr_low( void ) // called every 10ms
{
will need to be changed to:
void interrupt low_priority isr_low( void )
{
Quite a bit easier. The same goes for the high interrupt:
void interrupt isr_high (void)
{
Here we don’t have to specify high_priority, but if we want to we can. For the high priority (default) interrupt, omit the _low_priority keyword when defining the function; or you can use the high_priority keyword instead._
3.14.1 Variables and Functions
The MPLAB C18 #pragma sectiontype is used to allocate variables and code to an alternate section with the name specified. This is usually done so that this section can be explicitly linked in memory at a specific address or in an address range. If the aim is purely to locate a variable or function at a specific address, then the easiest way of performing this in MPLAB XC8 is to make the variable or function absolute, using the @ address construct.
Might be necessary for the pragma’s in the c018_reloc.c file. //TODO
3.19.2 Assembly Language Differences
Might be necessary for the inline assembly in the c018_reloc.c file. //TODO
C18 code in can18F66K80.c needs work. This code:
#if defined(MCHP_C18)
_asm
BCF COMSTAT, 6, 0
_endasm
#endif
needs addition of this code:
#if defined(__XC8)
#asm
BCF COMSTAT, 6, 0
#endasm
#endif
This code:
#if defined(MCHP_C18)
_asm
BCF COMSTAT, 7, 0
_endasm
#endif
needs addition of this code:
#if defined(__XC8)
#asm
BCF COMSTAT, 7, 0
#endasm
#endif
1.3.3 Options and Linker scripts
Is this an issue for the relocation files? //TODO
1.3.4 Assembly Code
Will need to change them //TODO
To be continued…